﻿using DevExpress.XtraEditors;
using Python.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Masuit;
using Masuit.Tools;

namespace CtyProject.UserControls
{
    public partial class PythonUc : DevExpress.XtraEditors.XtraUserControl
    {
        public PythonUc()
        {
            InitializeComponent();
            LoadPythonEnv();
        }
        private void LoadPythonEnv()
        {
            // 加载 Python 环境
            string pythonEnvPath = @"E:\Anaconda3\envs\ForNet";
            //Debug目录下的Python文件夹存放python代码
            string pythonSelfScriptPath = AppDomain.CurrentDomain.BaseDirectory + "Python";
            Runtime.PythonDLL = Path.Combine(pythonEnvPath, "python38.dll");
            PythonEngine.PythonHome = Path.Combine(pythonEnvPath, "python.exe");
            PythonEngine.PythonPath = $"{pythonEnvPath}\\Lib\\site-packages;{pythonEnvPath}\\Lib;" +
                $"{pythonEnvPath}\\DLLs;{pythonSelfScriptPath}";
            PythonEngine.Initialize();
        }
        //跨线程更新UI
        private void UpdateUi(Action action)
        {
            //判断UI控件关联的窗口句柄是否创建
            if (this.IsHandleCreated)
            {
                //Windows Forms的Invoke 方法提供了一种机制，使得即使从非UI线程发起调用，也能保证委托在UI线程上执行
                this.Invoke(action);
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                //保证多线程环境中安全地与Python解释器交互(通过获取GIL锁的方式)
                //使用using保证会自动释放锁
                using (Py.GIL())
                {
                    //dynamic类型表示运行时解析变量类型 (动态)
                    dynamic py = Py.Import("hello");
                    //数据类型转换使用
                    dynamic result = py.sayHello();
                    UpdateUi(() =>
                    {
                        memoEdit1.Text += result.ToString() + "\r\n";
                    });

                }
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            //Masuit.Tools.Net包用于方便类型转换
            try
            {
                using (Py.GIL()) {
                    dynamic py = Py.Import("hello");
                    dynamic result = py.add(textEdit1.Text.ToInt64(),textEdit2.Text.ToInt64());
                    UpdateUi(() =>
                    {
                        memoEdit1.Text += result.ToString() + "\r\n";
                    });
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            try
            {
                using (Py.GIL())
                {
                    //因为引入了python dll路径
                    dynamic np = Py.Import("numpy");
                    memoEdit1.Text += "cos(2pi):" + np.cos(np.pi * 2).ToString() + "\r\n";

                    dynamic sin = np.sin;
                    memoEdit1.Text += "sin(5):" + sin(5).ToString() + "\r\n";

                    dynamic a = np.array(new List<float> { 1, 2, 3 });
                    memoEdit1.Text += "List<float> type is:" + a.dtype.ToString() + "\r\n";
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
        }
    }
}
